<template>
  <div class="login-box">
    <div class="from-box">
      <div class="logo"><img src="@/assets/logo.png" alt="" /></div>
      <el-card>
        <el-form :model="form" :rules="rules" ref="loginRef">
          <el-form-item class="zh" prop="loginName">
            <el-input placeholder="输入账号" v-model="form.loginName">
              <template #prefix>
                <i class="el-icon-mobile-phone"></i>
              </template>
            </el-input>
          </el-form-item>

          <el-form-item prop="password">
            <el-input
              placeholder="输入密码"
              :type="isHidden ? 'password' : 'text'"
              v-model="form.password"
            >
              <template #prefix>
                <i class="el-icon-lock"></i>
              </template>
              <template #suffix>
                <i
                  :class="isHidden ? 'el-icon-minus' : 'el-icon-view'"
                  @click="isHidden = !isHidden"
                ></i>
              </template>
            </el-input>
          </el-form-item>

          <el-form-item prop="code" @keyup.enter.native="login">
            <el-input placeholder="输入验证码" v-model="form.code" class="code">
              <template #prefix>
                <i class="el-icon-check"></i>
              </template>
            </el-input>
            <div class="verification">
              <img :src="verificationImg" alt="" @click="update" />
            </div>
          </el-form-item>

          <el-form-item class="dl">
            <el-button type="primary" @click="login">登 录</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getCode } from '@/api/user'
export default {
  name: 'login-page',
  data() {
    return {
      verificationImg: '',
      form: {
        loginName: 'admin',
        password: 'admin',
        code: '',
        clientToken: Math.floor(Math.random() * 10 + 1),
        loginType: 0
      },
      rules: {
        loginName: [{ required: true, message: '请输入账号', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        code: [{ required: true, message: '请输入验证码', trigger: 'blur' }]
      },
      isHidden: true // 默认是隐藏的
    }
  },
  computed: {},

  methods: {
    async login() {
      try {
        // 表单校验
        await this.$refs.loginRef.validate()
        await this.$store.dispatch('user/login', this.form)
        await this.$store.dispatch('user/getUserInfo', 1)
        this.$router.push('/')
      } catch (err) {
        console.log(err)
      }
    },
    async update() {
      this.form.clientToken = Math.floor(Math.random() * 10 + 1)
      const res = await getCode(this.form.clientToken)
      // console.log(res)
      this.verificationImg = window.URL.createObjectURL(res)
      this.form.code = ''
    }
  },
  async created() {
    const res = await getCode(this.form.clientToken)
    // console.log(res)
    this.verificationImg = window.URL.createObjectURL(res)
  }
}
</script>

<style scoped lang="scss">
.login-box {
  width: 100vw;
  height: 100vh;
  background: url('@/assets/bg.png') no-repeat;
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
  .from-box {
    position: relative;
    width: 420px;
    .logo {
      position: absolute;
      text-align: center;
      top: -48px;
      left: 160px;
      img {
        width: 96px;
        height: 96px;
      }
    }
    .zh {
      padding-top: 50px;
    }
    .dl {
      text-align: center;
    }
  }
}

.verification {
  position: absolute;
  right: 0px;
  top: 0px;
}

.el-button--primary {
  color: #fff;
  background-color: #409eff;
  border-color: #409eff;
  width: 378px;
}

::v-deep .el-input--prefix .el-input__inner {
  padding-left: 30px;
  height: 50px;
}
</style>
